<?php
/**
 * 品牌活动车展项目
 * @author Administrator
 *
 */
class CarProject extends MY_Controller{
    function CarProject() {
        parent::MY_Controller();
    }
    /**
     * 添加或修改
     */
    function add(){
        $this->load->model('carProjectModel');
        if ($_POST){
            $id = (int)$this->input->post('id');
            $data = array(
                'storeId' => $this->user['userId'],
                'procId' => (int)$this->input->post('procId'),
                'levelId' => (int)$this->input->post('levelId'),
                'startTime' => $this->input->post('startTime'),
                'endTime' => $this->input->post('endTime'),
                'orderTarget' => (int)$this->input->post('orderTarget'),
                'capitalTarget' => (int)$this->input->post('capitalTarget'),
                'budgetApply' => (float)$this->input->post('budgetApply'),
                'remarks' => $this->input->post('remarks'),
                'budgetVoucher' => serialize(array()),
                'settleVoucher' => serialize(array()),
            );
            $data['storeId'] || showError('请选择所属专营店ID');
            $data['procId'] || showError('请选择所属主线流程');
            $data['levelId'] || showError('请选择车展级别');
            $data['startTime'] || showError('请选择开始时间');
            $data['endTime'] || showError('请选择结束时间');
            strtotime($data['startTime']) <= strtotime($data['endTime']) || showError('开始时间必须早于结束时间');
            //车展方案
            $budgetVoucher = (int)$this->input->post('budgetVoucher');
            $this->load->model('fileModel');
            $data['budgetVoucher'] = $this->fileModel->getData($budgetVoucher);
            $data['budgetVoucher'] || showError('请上传车展方案');
            $data['budgetVoucher'] = serialize($data['budgetVoucher']);
            $data['budgetApply'] > 0 || showError('请输入预算申请金额');
    
            if ($id){
                $findData = $this->carProjectModel->getData($id);
                if (in_array($findData['budgetAuditRs'], array(0,3))){
                    $data['budgetAuditRole'] = 0;
                    $data['budgetAuditRs'] = 0;
                }
                if ($this->carProjectModel->edit(array('id'=>$id,'storeId'=>$data['storeId']), $data)){
                    showSuccess('编辑成功');
                }else{
                    showError('编辑失败');
                }
            }else{
                $data['createTime'] = time();
                if ($this->carProjectModel->add($data)){
                    showSuccess('添加成功');
                }else{
                    showError('添加失败');
                }
            }
        }else{
            $procId = (int)$this->input->get('procId');
            $id = (int)$this->input->get('id');
            if ($id){
                $projectData = $this->carProjectModel->getNewData($id);
                $this->viewData['projectData'] = $projectData;
                $procId = (int)$projectData['procId'];
            }
            $this->load->model('carProcessModel');
            $procData = $this->carProcessModel->getData($procId);
            $procData || showError('主线流程数据不存在');
            $this->viewData['procData'] = $procData;
            $this->load->view('CarProject/add', $this->viewData);
        }
    }
    /**
     * 删除
     */
    function del(){
        $id = (int)$this->input->post('id');
        $id || showError('请选择要删除的车展基金');
        $this->load->model('carProjectModel');
        if($this->carProjectModel->del(array('id'=>$id, 'storeId'=>$this->user['userId']))){
            showSuccess('删除车展基金成功');
        }else{
            showError('删除车展基金失败');
        }
    }
    /**
     * 结算申请
     */
    function settleEdit(){
        $this->load->model('carProjectModel');
        if ($_POST){
            $id = (int)$this->input->post('id');
            $id || showError('请选择要申请结算的车展基金');
            $procId = (int)$this->input->post('procId');
            $procId || showError('请选择主线流程数据');
            $data = array(
                'orderActual' => (int)$this->input->post('orderActual'),
                'capitalActual' => (int)$this->input->post('capitalActual'),
                'settleApply' => (float)$this->input->post('settleApply'),
                'settleSubTime' => time(),
                'settleAuditRole' => 0,
                'settleAuditRsArea' => 0,
                'settleAuditRsHead' => 0,
            );
            $data['orderActual'] || showError('请输入订单实际达成');
            $data['capitalActual'] || showError('请输入留资实际达成');
            $data['settleApply'] || showError('请输入结算申请费用');
            //车展方案
            $settleVoucher = (int)$this->input->post('settleVoucher');
            $this->load->model('fileModel');
            $data['settleVoucher'] = $this->fileModel->getData($settleVoucher);
            $data['settleVoucher'] || showError('请上传车展方案');
            $data['settleVoucher'] = serialize($data['settleVoucher']);
            
            if ($this->carProjectModel->edit(array('id'=>$id,'storeId'=>$this->user['userId']),$data)){
                //更新金额
                $this->carProjectModel->updateMoney($id);
                //更新主线流程结算审核状态
                $this->load->model('carProcessModel');
                $this->carProcessModel->settleUpdateStatus($procId);
                showSuccess('结算申请成功');
            }else{
                showError('结算申请失败');
            }
        }else{
            $id = (int)$this->input->get('id');
            $id || showError('请选择要申请结算的车展基金');
            $projectData = $this->carProjectModel->getNewData(array('id'=>$id,'storeId'=>$this->user['userId']));
            $this->viewData['projectData'] = $projectData;
            $this->load->view('CarProject/settleEdit', $this->viewData);
        }
    }
}